package horncomb.bpm.process.def.state;

import horncomb.bpm.BpmContextHolder;
import horncomb.bpm.process.core.ExecutionContext;

/**
 * 子流程节点
 * <p>
 * 进入本节点后，将创建一个独立的子流程。
 * @author Daniel
 */
public class NestedProecessStateDefinition extends
		AbstractProcessStateDefinition {
	private static final long serialVersionUID = 3625405917612293518L;

	/**
	 * @param id
	 */
	public NestedProecessStateDefinition(String id) {
		super(id);
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected void doEnter(ExecutionContext context) {
		super.doEnter(context);
		// 自动启动子流程
		BpmContextHolder.getContext().getProcessManagementService().start(
				context.getToken(), this.getInnerProcessDefinitionId(),
				context.getContextModel());
	}

	public boolean checkBeforeLeaving(ExecutionContext context) {
		return super.checkBeforeLeaving(context)
				&& BpmContextHolder.getContext().getProcessManagementService()
						.hasNestedProcessInstance(context.getToken().getId());
	}
}
